/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.util.pager;

import java.util.Collection;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author makcro
 */
public abstract class AbstractPager {

    protected int firstResult;
    protected int maxResultsPerPage;
    protected Long resultsTotal;
    protected boolean showNext;
    protected boolean showPrev;
    protected final String paramPage = "p";
    protected final String paramDirection = "d";
    protected final String paramDirectionValueNext = "n";
    protected final String paramDirectionValuePrev = "p";
    protected int pageNumber = 1;
    protected String orederByClause = null;

    public AbstractPager() {

        this.firstResult = 0;


    }

    public void doPaging(HttpServletRequest request) {

        setPageSize();

        this.firstResult = (this.pageNumber - 1) * this.maxResultsPerPage;
        setIndicators();

        injectData(request);

    }

    protected void setIndicators() {
        if (firstResult == 0 && resultsTotal > maxResultsPerPage) {
            this.showPrev = false;
            this.showNext = true;
        } else if (firstResult != 0 && resultsTotal - firstResult <= maxResultsPerPage) {
            this.showNext = false;
            this.showPrev = true;
        } else if (firstResult != 0 && resultsTotal - firstResult > maxResultsPerPage) {
            this.showNext = true;
            this.showPrev = true;
        }

    }

    protected abstract void injectData(HttpServletRequest request);

    protected Collection generatePageNumbers() {
        Collection<Integer> result = new Vector<Integer>();
        int numberOfPages = resultsTotal.intValue() / maxResultsPerPage;
        int rest = resultsTotal.intValue() % maxResultsPerPage;

        for (int i = 0; i < numberOfPages; i++) {
            //povecavamo za jedan da bi ispis strana pocinjao od 1
            result.add(new Integer(i + 1));
        }
        if (rest != 0) {
            //takodje povecavamo i ovde
            result.add(numberOfPages + 1);
        }
        return result;
    }

    protected abstract void setPageSize();

    public int getFirstResult() {
        return firstResult;
    }

    public void setFirstResult(int firstResult) {
        this.firstResult = firstResult;
    }

    public int getMaxResults() {
        return maxResultsPerPage;
    }

    public void setMaxResults(int maxResults) {
        this.maxResultsPerPage = maxResults;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Long getResultsTotal() {
        return resultsTotal;
    }

 
    public String getItemInterval(){
        StringBuilder itemInterval = new StringBuilder();
        if(resultsTotal == 0){
            itemInterval.append(0);
        }
        else if(resultsTotal <= maxResultsPerPage){
            itemInterval.append("1").append("..").append(resultsTotal);
        }
        else if(pageNumber == 1){
            itemInterval.append("1").append("..").append(pageNumber * maxResultsPerPage);
        }
        else if (pageNumber * maxResultsPerPage > resultsTotal){
            itemInterval.append((pageNumber - 1) * maxResultsPerPage).append("..").append(resultsTotal);
        }
        else {
            itemInterval.append((pageNumber - 1) * maxResultsPerPage).append("..").append(pageNumber * maxResultsPerPage);
        }
        
        return itemInterval.toString();
    }

}
